package com.qiyuesuo.exception.handler;

import com.qiyuesuo.exception.BusinessException;
import com.qiyuesuo.exception.code.BaseResponseCode;
import com.qiyuesuo.utils.DataResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

/**
 * @ClassName: RestExceptionHandler
 * TODO:统一的异常处理类
 * @Author: ZNQ
 * @UpdateUser: ZNQ
 * @Version: 0.0.1
 */
//@RestControllerAdvice:表明此类为全局异常处理的类
@RestControllerAdvice
@Slf4j
public class RestExceptionHandler {

    //@ExceptionHandler:声明异常处理的方法
    //处理Exception异常
    @ExceptionHandler(value = Exception.class)
    public DataResult exception(Exception e){
        log.error("Exception,{},{}",e.getLocalizedMessage(),e);
        return DataResult.getResult(BaseResponseCode.SYSTEM_ERROR);
    }

    //处理自定义运行时异常
    @ExceptionHandler(value = BusinessException.class)
    public DataResult businessException(BusinessException e){
        log.error("businessException,{},{}",e.getLocalizedMessage(),e);
        return DataResult.getResult(e.getCode(),e.getMsg());
    }

    //上传文件超出指定大小异常
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public DataResult maxUploadSizeExceededException(MaxUploadSizeExceededException e){
        log.error("MaxUploadSizeExceededException,{},{}",e,e.getLocalizedMessage());
        return DataResult.getResult(BaseResponseCode.FILE_TOO_LARGE);
    }
}
